﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：科目选择
 */
var dfopid = request('dfopid');
var selectValue = request('selectValue');
var selectedRow;
var refreshGirdData;
var acceptClick;

var companyId = request("companyId");
var ztId = request("ztId");
var kjnd = request("kjnd");
var keyword = request("keyword");
var controlName = request("controlName");
var needMx = request("needMx");
var btnIndex = request("btnIndex");

var bootstrap = function ($, learun) {
    "use strict";
    var kmitem = {value: '', text: ''};

    var page = {
        init: function () {
            page.bind();
            page.initGrid();
            page.search({keyword: keyword});
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            $("#txt_Keyword").keypress(function (even) {
                if (even.which == 13) {
                    var keyword = $(this).val();
                    page.search({keyword: keyword});
                }
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'kjkms/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                    {
                        label: "余额方向", name: "yefx", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "借" : "贷";
                        }
                    },
                    {
                        label: '是否明细', name: 'isLeaf', width: 1, align: 'left', hidden: true,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "是" : "";
                        }
                    },
                    {label: '编号', name: 'chrCode', width: 1, align: 'left', hidden: true},
                    {label: '简称', name: 'chrName', width: 1, align: 'left', hidden: true}
                ],
                dblclick: function (row) {
                    if (needMx == "0" || row.isMx == 1) {
                        kmitem = {value: row.code, text: row.simpleName};
                        if (learun.frameTab.currentIframe().kjkm != undefined) {
                            learun.frameTab.currentIframe().kjkm = kmitem;
                        }
                        if(!!btnIndex){
                            parent.$(".layui-layer-btn0")[btnIndex].click();
                        } else {
                            parent.$(".layui-layer-btn0")[1].click();
                        }
                    } else {
                        learun.alert.warning('只能选择末级科目！');
                        return;
                    }

                    //关闭layer弹出层
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.status = 1;
            // param.keyword = keyword;
            $('#gridtable').jfGridSet('reload', param);
        }
    };
    refreshGirdData = function () {
        page.search();
    };

    // 保存数据
    acceptClick = function (callBack) {
        var row = $('#gridtable').jfGridGet('rowdata');
        if (row.code) {
            if (needMx == "0" || row.isMx == 1) {
                kmitem = {value: row.code, text: row.simpleName};
                callBack(kmitem, dfopid);
                return true;
            } else {
                learun.alert.warning('只能选择末级科目！');
            }
        } else {
            learun.alert.warning('请选择科目！');
        }

    };
    page.init();
}